package study.intconsulting.transform;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import study.intconsulting.net.io.HongClass;
import study.intconsulting.vo.PcInfo;


public class Trans_3rd {

	
	public PcInfo mh(PcInfo pcInfo){
		String resultMsg = null;
		PcInfo resultPcInfo = null;
		
		HongClass hongClass = new HongClass();
		
		resultMsg = hongClass.call(makeXML(pcInfo));
		
		
		if(resultMsg != null) {
			resultPcInfo = makePcInfo(resultMsg);
		} else {
			resultPcInfo = new PcInfo();
		}
		
		
		
		return resultPcInfo;
	}
	
	
	
	
	public String makeXML(PcInfo pcInfo){
		StringBuffer xml = new StringBuffer();
		
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\n");
		if(pcInfo == null){
			xml.append("<pcInfo/>").append("\n");
		} else {
			xml.append("<pcInfo>").append("\n");
			xml.append(assembleTag("ip", pcInfo.getIp())).append("\n");
			xml.append(assembleTag("name", pcInfo.getName())).append("\n");
			xml.append(assembleTag("who", pcInfo.getWho())).append("\n");
			xml.append("</pcInfo>").append("\n");
		}
		
		return xml.toString();
	}
	
	public PcInfo makePcInfo(String xmlString) {
		PcInfo pcInfo = new PcInfo();
		
		if(xmlString == null || xmlString.length() == 0){
			return pcInfo;
		}
		
		ByteArrayInputStream input = new ByteArrayInputStream(xmlString.getBytes());
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		
		try {
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(input);
			
			pcInfo.setIp(nodeValue(doc.getElementsByTagName("ip")));
			pcInfo.setName(nodeValue(doc.getElementsByTagName("name")));
			pcInfo.setWho(nodeValue(doc.getElementsByTagName("who")));
			
		} catch (ParserConfigurationException e) {
			// docBuilderFactory.newDocumentBuilder
			e.printStackTrace();
		} catch (SAXException e) {
			// docBuilder.parse
			e.printStackTrace();
		} catch (IOException e) {
			// docBuilder.parse
			e.printStackTrace();
		} finally{
			try {
				if(input != null) input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return pcInfo;
		
		
	}
	
	// 태그 만들기
	public String assembleTag(String tagName, String tagValue){
		StringBuffer tag = new StringBuffer();
		if(tagValue != null && tagValue.length() > 0){
			tag.append("<").append(tagName).append(">").append(tagValue).append("</").append(tagName).append(">");
		} else {
			tag.append("<").append(tagName).append("/>");
		}
		return tag.toString();
	}
	
	// 태그 값 가져오기
	public String nodeValue(NodeList nodeList){
		String value = "";
		if(nodeList != null && nodeList.getLength() > 0 && nodeList.item(0).hasChildNodes()){
			value = nodeList.item(0).getFirstChild().getNodeValue();
		}
		return value;
	}
	
	public static void main(String[] arg){
		Trans_3rd trans = new Trans_3rd();
		PcInfo inputPcInfo = new PcInfo();
		PcInfo outputPcInfo = null;
		
		inputPcInfo.setIp("localHost");
		inputPcInfo.setName("test");
		inputPcInfo.setWho("나");
		
		
		
		outputPcInfo = trans.mh(inputPcInfo);
		
		System.out.println(outputPcInfo);
		
	}
	
	
}
